<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;

class RankController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
          $db = \DB::table("rank")->orderBy("num","desc");
         //$db = \DB::table("rank");
       $list = $db->paginate(5); //5条每页浏览
        
       return view("admin.rank.index",['list'=>$list]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("admin.rank.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       //dd($_POST);
	   $data = $_POST;
	   unset($data['_token']);
	   
	   $id = \DB::table("rank")->insertGetId($data);
	   //判断
        if($id>0){
            $info = "新书排行信息添加成功！";
        }else{
            $info = "新书排行信息添加失败！";
        }
        
        return view("admin.rank.info",['info'=>$info]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $list = \DB::table('rank')->where('id',$id)->first();
        return view('admin.rank.edit',['list'=>$list]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $_POST;
		unset($data['_token']);
		unset($data['_method']);
		
		$id = \DB::table("rank")->where("id",$id)->update($data);
		
		//判断
        if($id>0){
            $info = "新书排行信息修改成功！";
        }else{
            $info = "新书排行信息修改失败！";
        }
        
        return view("admin.rank.info",['info'=>$info]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
         \DB::table('rank')->delete($id);
        return redirect("admin/rank");
    }
}
